package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.constant.LoginConstans;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.system.mapper.SysUserMapper;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.AlertDataMapper;
import com.zzyl.serve.domain.AlertData;
import com.zzyl.serve.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 * 
 * @author iteheima
 * @date 2025-03-18
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private ISysUserService userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
//        获取redis中的用户id
        Long userLoginId = (Long) redisTemplate.opsForValue().get(LoginConstans.LOGIN_USER);
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }
    /**
     * 处理设备报警数据
     */
    @Override
    public void handleAlertData(AlertData alertData) {
//        查询报警数据
        AlertData alertData1 = alertDataMapper.selectOne(new LambdaQueryWrapper<AlertData>()
                .eq(AlertData::getId, alertData.getId()));
        if (alertData1 == null) {
            return;
        }
//        赋值
        alertData1.setProcessingResult(alertData.getProcessingResult());
        alertData1.setProcessingTime(alertData.getProcessingTime());
//        查询用户名称
        SysUser sysUser = userMapper.selectUserById(alertData1.getUserId());
        if (ObjectUtil.isEmpty(sysUser)){
            return;
        }
        String nickName = sysUser.getNickName();

        alertData1.setProcessorId(alertData1.getUserId());
        alertData1.setProcessorName(nickName);
        alertData1.setStatus(1);
        alertDataMapper.updateAlertData(alertData1);
    }
}
